package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.entity.Help;
import com.seecen.service.HelpService;
import com.seecen.mapper.HelpMapper;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author xuxin
* @description 针对表【t_help(使用帮助文档表)】的数据库操作Service实现
* @createDate 2025-10-16 10:19:26
*/
@Service
@Slf4j
public class HelpServiceImpl extends ServiceImpl<HelpMapper, Help>
    implements HelpService{

    @Autowired
    private HelpMapper helpMapper;

    @Override
    public Result getHelpList() {
        try {
            LambdaQueryWrapper<Help> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Help::getIsDeleted, 0)
                    .orderByAsc(Help::getId);

            List<Help> helpList = helpMapper.selectList(wrapper);

            log.info("获取帮助文档列表成功，共 {} 条", helpList.size());

            HashMap<String, Object> data = new HashMap<>();
            data.put("helpList", helpList);
            data.put("total", helpList.size());

            return Result.ok(data);
        } catch (Exception e) {
            log.error("获取帮助文档列表失败: {}", e.getMessage());
            return Result.build(null, ResultCode.USE_HELP_ERROR);
        }
    }

    @Override
    public Result getHelpById(Long id) {
        try {
            if (id == null || id <= 0) {
                log.warn("获取帮助文档详情失败：ID不合法");
                return Result.build(null, ResultCode.USE_HELP_ID_ERROR);
            }

            Help help = helpMapper.selectById(id);

            if (help == null || help.getIsDeleted() == 1) {
                log.warn("获取帮助文档详情失败：文档不存在或已删除，ID: {}", id);
                return Result.build(null, ResultCode.USE_HELP_ERROR);
            }

            log.info("获取帮助文档详情成功，ID: {}, 标题: {}", id, help.getTitle());

            HashMap<String, Object> data = new HashMap<>();
            data.put("help", help);

            return Result.ok(data);
        } catch (Exception e) {
            log.error("获取帮助文档详情失败: {}", e.getMessage());
            return Result.build(null, ResultCode.USE_HELP_DETAIL_ERROR);
        }
    }
}




